<!--自封有表单提交数据管理-->
<template>
  <div id="businessFormValue" class="app-container  flex-row">
    <div class="conmmon-container-left scroll__y__content scrollbar-no-width" style="width:300px">
      <div class="common-title">
        <h2>表单</h2>
      </div>
      <el-input v-model="searchValue" :placeholder="'请输入表单名称'" style="margin-top: 10px" />
      <el-tree
        ref="formTree"
        class="common-el-tree tree-active"
        :data="treeDatas"
        :props="defaultTreeProps"
        node-key="id"
        :filter-node-method="onTreeFilter"
        :expand-on-click-node="false"
        default-expand-all
        :current-node-key="nodeSelectedKey"
        @node-click="handleFormNodeClick">
        <span slot-scope="{ data}" class="custom-tree-node">
          <img v-if="data.ext==='2'" :src="common.getFileView(data.icon)" style="width:22px;height:22px;margin-right: 5px;border-radius: 11px;" @error="handleImageError">
          <span v-if="data.ext==='1'" class="text" style="font-weight:bold;font-size: 14px;">{{ data.label }}</span>
          <span v-else class="text">{{ data.label }}</span>
        </span>
      </el-tree>
    </div>
    <div class="conmmon-container-main flex-main">
      <!--搜索条件-->
      <div class="filter-container">
        <el-form :model="listQuery" :inline="true">
          <el-form-item>
            <el-input v-model="listQuery.realName" clearable placeholder="请输入真实姓名" @keyup.enter.native="handleFilter" />
            <el-input v-model="listQuery.mobile" clearable placeholder="请输入手机号" @keyup.enter.native="handleFilter" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
            <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!--列表-->
      <div class="table-container flex-main">
        <div class="table-container-header">
          <table-toolbar :create="false" @refresh="handleFilter" @remove="removeRecord">
            <template slot="rightTool">
              <export-drop-down class-name="businessFormValue" :list-query="listQuery" async text="导出当前表单数据" />
            </template>
          </table-toolbar>
        </div>
        <el-table
          ref="multipleTable"
          :data="list"
          header-cell-class-name="table-header"
          fit
          height="100%"
          highlight-current-row
          @selection-change="handleSelectionChange"
          @row-click="handleRowClick"
          @sort-change="sortChange">
          <el-table-column type="selection" width="60" />
          <el-table-column align="center" prop="id" label="#" width="60">
            <template slot-scope="scope">{{ scope.$index+1 }}</template>
          </el-table-column>
          <el-table-column prop="createdBy" label="提交人" width="230" :show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.createdBy | optionUserFormat }}<br>{{ scope.row.gmtCreated | formatDate }}</template>
          </el-table-column>
          <el-table-column label="手机号" prop="createdUserMobile" width="100" show-overflow-tooltip />
          <el-table-column v-for="(item,i) in tableColumns" :key="i" :label="item.label" :prop="item.prop" min-width="150" :show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row[item.prop] }}</template>
          </el-table-column>
          <el-table-column prop="gmtModified" sortable label="修改时间" width="160">
            <template slot-scope="scope">{{ scope.row.gmtModified | formatDate }}</template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination_handler">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},

  data() {
    return {
      nodeSelectedKey: '',
      defaultTreeProps: {
        children: 'children',
        label: 'label',
        isLeaf: 'leaf'
      },
      searchValue: '',
      treeDatas: [],
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        realName: null,
        mobile: null,
        sort: this.common.SORT_DEFAULT
      },

      multipleSelectionRecords: [],
      tableColumns: []
    }
  },
  watch: {
    searchValue(val) {
      this.$refs.formTree.filter(val)
    }
  },
  created() {
    this.fetchForms()
  },
  methods: {
    fetchForms() {
      this.$store.dispatch('businessFormTree').then((res) => {
        this.treeDatas = []
        res.forEach(item => {
          this.treeDatas.push({
            ...item,
            disabled: true
          })
        })
        if (this.treeDatas.length > 0) {
          if (this.treeDatas[0].children.length > 0) {
            var nodeSelectedKey = this.treeDatas[0].children[0].id
            this.$refs.formTree.setCheckedKeys([nodeSelectedKey])
            this.$nextTick(() => {
              this.handleFormNodeClick(this.treeDatas[0].children[0], this.$refs.formTree.getNode(nodeSelectedKey))
            })
          }
        }
      }).catch((_) => {})
    },
    handleFormNodeClick(data) {
      if (data.ext === '1') {
        return
      }
      this.nodeSelectedKey = data.id
      this.$refs.formTree.setCurrentKey(data.id)
      this.listQuery = {
        ...this.listQuery,
        formId: data.id
      }
      this.fetchFromDetails()
    },
    fetchFromDetails() {
      this.$store.dispatch('businessFormGetById', this.listQuery.formId).then((res) => {
        if (res.formContent) {
          var formContent = JSON.parse(res.formContent)
          this.tableColumns = []
          formContent.forEach(item => {
            this.tableColumns.push({
              prop: item.field,
              label: item.title,
              type: item.type
            })
          })
          this.handleReset()
        } else {
          this.msgWarning('暂未设置提交表单')
        }
      })
    },

    /* 节点过滤函数 */
    onTreeFilter(value, data) {
      if (!value) return true
      return data[this.defaultTreeProps.filter]
        ? data[this.defaultTreeProps.label].indexOf(value) !== -1 ||
              data[this.defaultTreeProps.filter].indexOf(value.toUpperCase()) !==
                -1
        : data[this.defaultTreeProps.label].indexOf(value) !== -1
    },
    // 表格自带方法开始
    /* 全选/反选*/
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    /* 表格行数据单击*/
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /* 表格列排序*/
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    // 表格自带方法结束

    /* 搜索*/
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /* 重置*/
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        realName: null,
        mobile: null,
        sort: this.common.SORT_DEFAULT
      }
      this.$refs.multipleTable.clearFilter()
      this.$refs.multipleTable.clearSort()
      this.fetchData()
    },
    async fetchData() {
      const response = await this.$store.dispatch('businessFormValuePageV1', this.listQuery)
      this.list = []
      response.records.forEach(item => {
        var obj = { ...item }
        if (item.formValue) {
          var json = JSON.parse(item.formValue)
          for (var index in json) {
            obj[index] = json[index]
          }
        }
        this.list.push(obj)
      })
      this.total = response.total
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
